﻿namespace dogd
{
	using System;
	using System.Collections.Generic;

	/// <summary>
	/// MetadataProvider provides caching for TableMetadata instances.
	/// </summary>
	public class MetadataProvider : IMetadataProvider
	{
		/// <summary>
		/// Initialize with default values.
		/// </summary>
		public MetadataProvider()
		{
			this.MetadataDictionary = new Dictionary<Type, TableMetadata>();
		}

		/// <summary>
		/// Dictionary of metadata keyed against the persisted object type.
		/// </summary>
		private Dictionary<Type, TableMetadata> MetadataDictionary
		{
			get;
			set;
		}

		/// <summary>
		/// Loads metadata for a given type.
		/// </summary>
		/// <param name="dataType"></param>
		/// <returns></returns>
		public TableMetadata LoadDataFor(Type dataType)
		{
			TableMetadata data = null;
			this.MetadataDictionary.TryGetValue(dataType, out data);

			if (data == null)
			{
				data = new TableMetadata(dataType);
				this.MetadataDictionary.Add(dataType, data);
			}

			return data;
		}
	}
}
